package com.hboxs.asl.controller.app.member;

import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.controller.app.BaseController;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.entity.Question;
import com.hboxs.asl.service.MemberService;
import com.hboxs.asl.service.QuestionService;
import com.hboxs.asl.service.QuestionTradeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Controller(value = "appMQuestionTradeController")
@RequestMapping(value = "/app/m/questionTrade")
public class QuestionTradeController extends BaseController {

    @Resource(name = "memberServiceImpl")
    private MemberService memberService;
    @Resource(name = "questionServiceImpl")
    private QuestionService questionService;
    @Resource(name = "questionTradeServiceImpl")
    private QuestionTradeService questionTradeService;


    @RequestMapping(value = "/apply_refund", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage applyRefund(Long questionId) {
        try {
            Member member = memberService.getCurrentByToken();
            if (member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            Question question = questionService.find(questionId);
            if (question == null) {
                return JsonMessage.error("找不到该提问");
            }
            if (question.getPayPrice().compareTo(BigDecimal.ZERO) == 0) {
                return JsonMessage.error("该提问是免费的，不能申请退款");
            }
            if (question.getPaymentStatus().equals(Question.PaymentStatus.refund)) {
                return JsonMessage.error("您已提交申请，请等候退款");
            }
            if (question.getPaymentStatus().equals(Question.PaymentStatus.refundSuccess)) {
                return JsonMessage.error("已经退款成功");
            }
            Long time = question.getCreateDate().getTime();
            Long currentTime = System.currentTimeMillis();
            Long millisecond = 24*60*60*1000l;
            Long gap = currentTime - time;
            if (gap < millisecond) {
                return JsonMessage.error("提问24小时后才可以申请退款");
            }
            question.setPaymentStatus(Question.PaymentStatus.refund);
            questionService.update(question);
            return JsonMessage.success("申请成功，请等待审核");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 支付提问
     * @param questionId
     * @return
     */
    @RequestMapping(value = "/generate_question_trade", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage questionTrade(Long questionId) {
        try {
            Member member = memberService.getCurrentByToken();
            if (member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            Question question = questionService.find(questionId);
            if (question == null) {
                return JsonMessage.error("该易问易答不存在");
            }
            // 生成订单返回订单编号
            String sn = questionTradeService.generateTrade(question);
            JsonMessage.success("sn", sn);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

}
